package com.huatai.datacenter.controller.datamodel;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.datacenter.entity.datamodel.MappingModelEntity;
import com.huatai.datacenter.service.MappingModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 数据模型映射管理Controller
 *
 * @author lizhong
 * @date 2023年8月2日
 */
@RestController
@RequestMapping("/dataModel/mapping")
@Api(value = "映射模型模块", tags = "映射模型")
public class MappingModelController {

	@Autowired
	private MappingModelService mappingModelService;

	/**
	 * 获取所有映射模型
	 *
	 * @param mappingModelEntity 分页查询参数类
	 * @return 数据模型-映射模型列表
	 * @author lizhong
	 * @date 2023年8月2日
	 */
	@PostMapping("/getList")
	@ApiOperation("获取映射模型")
	@ApiOperationSupport(order = 1, includeParameters = {
		"mappingModelEntity.pageIndex",
		"mappingModelEntity.pageSize",
		"mappingModelEntity.mappingModelName"
	})
	public R<Page<MappingModelEntity>> getList(@RequestBody MappingModelEntity mappingModelEntity) {
		Page<MappingModelEntity> dataModelEntityPage = mappingModelService.selectMappingModelList(mappingModelEntity);
		return R.data(dataModelEntityPage, "获取数据模型成功");
	}

	/**
	 * 保存映射模型
	 *
	 * @param mappingModelEntity 新的映射模型，包括字段
	 * @return R
	 * @author lizhong
	 * @date 2023年8月2日
	 */
	@PostMapping("/saveOne")
	@ApiOperation("保存映射模型")
	@ApiOperationSupport(order = 2, includeParameters = {
		"mappingModelEntity.mappingModelName",
		"mappingModelEntity.dataModelId", // 数据模型Id
		"mappingModelEntity.dataModelName", // 数据模型Id
		"mappingModelEntity.fields.fieldName", // 映射模型字段
		"mappingModelEntity.fields.fieldCnName",
		"mappingModelEntity.fields.dataModelFieldId", // 对应的数据模型字段
		"mappingModelEntity.fields.dataModelFieldName",
		"mappingModelEntity.fields.dataModelFieldCnName"
	})
	public R saveOne(@RequestBody MappingModelEntity mappingModelEntity) {
		return R.status(mappingModelService.insertMappingModel(mappingModelEntity));
	}

	/**
	 * 根据id修改映射模型
	 *
	 * @param mappingModelEntity 新的映射模型
	 * @return
	 * @author lizhong
	 * @date 2023年8月3日
	 */
	@PostMapping("/updateById")
	@ApiOperation("修改映射模型")
	@ApiOperationSupport(order = 3, includeParameters = {
		"mappingModelEntity.id",
		"mappingModelEntity.mappingModelName",
		"mappingModelEntity.fields.id", // 这个要保留 修改时字段id必传
		"mappingModelEntity.fields.fieldName",
		"mappingModelEntity.fields.fieldCnName",
		"mappingModelEntity.fields.dataModelFieldId", // 对应的数据模型字段
		"mappingModelEntity.fields.dataModelFieldName",
		"mappingModelEntity.fields.dataModelFieldCnName",
		"mappingModelEntity.examples"
	})
	public R<MappingModelEntity> updateById(@Valid @RequestBody MappingModelEntity mappingModelEntity) {
		mappingModelEntity.setUpdateTime(null);
		mappingModelEntity.setUpdateUserid(null);
		mappingModelEntity.setDeleted(null);
		return R.status(mappingModelService.updateMappingModel(mappingModelEntity));
	}

	/**
	 * 根据id删除映射模型
	 *
	 * @param mappingModelEntities
	 * @return
	 * @date 2023年8月3日
	 */
	@PostMapping("/deleteByIds")
	@ApiOperation("删除映射模型")
	@ApiOperationSupport(order = 4, includeParameters = "mappingModelEntities.id")
	public R<MappingModelEntity> deleteByIds(@Valid @RequestBody List<MappingModelEntity> mappingModelEntities) {
		return R.status(mappingModelService.deleteByIds(mappingModelEntities));
	}
}
